﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Merge
{
    public class TidyRule
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger
(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public TidyRule(ISet<string> sources, string to)
        {
            this.Sources = sources;
            this.To = to;
        }

        public ISet<string> Sources { get; set; }
        public string To { get; set; }

        public static TidyRule FromConfigLine(string configLine)
        {
            //镜面：镜钢   8k
            if (string.IsNullOrEmpty(configLine))
                return null;
            if (!(configLine.Contains(":") || configLine.Contains("：")))
            {
                return null;
            }
            string[] parts = configLine.Split(new char[] { ':', '：' });
            string to = parts[0].Trim();
            string[] sources = parts[1].Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            if (sources.Length == 0) return null;
            return new TidyRule(sources.ToHashSet(), to);
        }

        public override string ToString()
        {
            return string.Join(",", this.Sources) + "->" + this.To;
        }
    }
}
